package eleanor.ltd.isRealEstate.security;

import java.io.IOException;

import org.apache.tapestry5.Link;
import org.apache.tapestry5.runtime.Component;
import org.apache.tapestry5.services.ComponentEventRequestParameters;
import org.apache.tapestry5.services.ComponentRequestFilter;
import org.apache.tapestry5.services.ComponentRequestHandler;
import org.apache.tapestry5.services.ComponentSource;
import org.apache.tapestry5.services.PageRenderLinkSource;
import org.apache.tapestry5.services.PageRenderRequestParameters;
import org.apache.tapestry5.services.Response;

import eleanor.ltd.isRealEstate.annotations.AdministratorAccess;
import eleanor.ltd.isRealEstate.annotations.BuyerAccess;
import eleanor.ltd.isRealEstate.pages.Contact;
import eleanor.ltd.isRealEstate.pages.Index;
import eleanor.ltd.isRealEstate.pages.Login;
import eleanor.ltd.isRealEstate.services.Authenticator;

public class AuthenticationFilter implements ComponentRequestFilter {

	private final PageRenderLinkSource renderLinkSource;

    private final ComponentSource componentSource;

    private final Response response;

    private final Authenticator authenticator;

    private String IndexPage = Index.class.getSimpleName();

    private String loginPage = Login.class.getSimpleName();

    public AuthenticationFilter(PageRenderLinkSource renderLinkSource,
                    ComponentSource componentSource, Response response,
                    Authenticator authenticator) {
            this.renderLinkSource = renderLinkSource;
            this.componentSource = componentSource;
            this.response = response;
            this.authenticator = authenticator;
    }

    public void handleComponentEvent(
                    ComponentEventRequestParameters parameters,
                    ComponentRequestHandler handler) throws IOException {

            if (dispatchedToLoginPage(parameters.getActivePageName())) {
                    return;
            }

            handler.handleComponentEvent(parameters);

    }

    public void handlePageRender(PageRenderRequestParameters parameters,
                    ComponentRequestHandler handler) throws IOException {

            if (dispatchedToLoginPage(parameters.getLogicalPageName())) {
                    return;
            }

            handler.handlePageRender(parameters);
    }

    private boolean dispatchedToLoginPage(String pageName) throws IOException {
    		Component pageComponent = componentSource.getPage(pageName);
    		Link link = renderLinkSource.createPageRenderLink("Login");
    		Link linkToIndex = renderLinkSource.createPageRenderLink("Index");
            System.out.println("PAGE NAME STRANICE: " + pageName);
            boolean isLogged = authenticator.isLoggedIn();
            System.out.println("" + isLogged);
            
            if (authenticator.isLoggedIn()) {
    			
            	if(pageComponent.getClass().isAnnotationPresent(AdministratorAccess.class)) {
            		if(authenticator.isAdmin()) {
            			System.out.println("dasdfdaohfoafaoidfaugdfodagsogfadofdgaofjbdjabjfdavfvavdadivfuafbadfbfbpausvfuadbfbapuvfpadvsfuavdpuvfpadsupadpvuasfu");
            		}
            		
            		return false;
            	}
    			return false;

    		} 
            System.out.println("User nije logovan na sistem!");
            
            
            if(pageComponent.getClass().isAnnotationPresent(BuyerAccess.class)) {
        		return false;
        	}
//            Link login = renderLinkSource.createPageRenderLink("login/Login");
    		response.sendRedirect(link);
    		return true;
    		
            
            
    }

}
